<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>뉴스레터</title>
</head>
<body>
<%
if(request.getParameter("mode")==null){
%>
<script type="text/javascript">
	window.alert('정상적인 경로를 통해서 접근해 주세요.'); 
	window.location='news_letter.jsp';
</script>
<%}else{
	String driver="oracle.jdbc.driver.OracleDriver";
	String url="jdbc:oracle:thin:@localhost:1521:xe";
	String user="pm";
	String pass="oracle";
	String name=request.getParameter("name");
	String email=request.getParameter("email");
	String fpage=request.getParameter("fpage");
	String mode=request.getParameter("mode");
	System.out.println(name);
	System.out.println(email);
	System.out.println(fpage);
	System.out.println(mode);
	int result=0;
	try{
		Class.forName(driver);
		Connection con=DriverManager.getConnection(url,user,pass);
		String sql=null;
		PreparedStatement pstmt=null;
			if(mode.equals("insert")){
				sql="insert into news_letter values (?, ?,sysdate,'Y')";
				pstmt=con.prepareStatement(sql);
				pstmt.setString(1, name);	
				pstmt.setString(2, email);	
			}else if(mode.equals("delete")){
				sql="delete from news_letter where email_v=?";
				pstmt=con.prepareStatement(sql);
				pstmt.setString(1, email);	
			}
		result=pstmt.executeUpdate();
		System.out.println(result);
		
		pstmt.close();
			con.close();
	}catch(SQLException e){%>
<script type="text/javascript">
	window.alert('중복되는 이메일 주소입니다.'); 
	window.location='news_letter.jsp';
</script>
	<%} 
		
if(mode.equals("insert")){%>
<script type="text/javascript">
	window.alert('정책소식지 구독신청이 완료 되었습니다.\r\n\r\n다음 정책소식지부터 이메일로 받아보실 수 있습니다.'); 
	window.location='news_letter.jsp';
</script>
<%}else if(mode.equals("delete") && result==1){%>
<script type="text/javascript">
	window.alert('정책소식지 구독해지가 완료되었습니다.\r\n\r\n언제든지 재신청이 가능하십니다.'); 
	window.location='news_letter.jsp';
</script>
<%}else if(mode.equals("delete") && result==0){%>
<script type="text/javascript">
	window.alert('이미 해지 신청이 되었거나\r\n\r\n정보를 찾을 수 없습니다.'); 
	window.location='news_letter.jsp';
</script>
<%} 
}
%>







</body>
</html>